<?php
class Tools_ImageController extends Promotor_Controller_Action
{
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_tools_image',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),
		
		'display' => array(
			'disableTemplate' => true
		)
	);

	public $acl = array(
		'index' => 'update',
		'display' => 'update'
	);

	/**
	 * Wyświetlanie opcji modyfikacji
	 * i zapisywanie gdy dane post! 
	 */
	public function indexAction()
	{
		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();
		$image = $rq->getQuery('image');

		// przekaż dane  do widoku
		$this->view->image = $image;
		
		if (!$rq->isPost()) {
			return;
		}
		
		$model = new Tools_Model_Image($image);
		$model->setActions($rq->getParams());
		$model->save();
		
		$status = $model->getStatus();
		$messages = $model->getMessages();
		
		/* @var $flashMessenger Zend_Controller_Action_Helper_FlashMessenger */
		$flashMessenger = $this->_helper->getHelper('FlashMessenger');
		$flashMessenger->addMessage($status);
		array_map(array($flashMessenger, 'addMessage'), $messages);
		
		/* @var $url Zend_Controller_Action_Helper_Url */
		$url = $this->_helper->getHelper('Url');

		$redirectUrl = $url->simple('index');
		$redirectUrl .= sprintf('?image=%s', $image);
		
		/* @var $redirector Zend_Controller_Action_Helper_Redirector */
		$redirector = $this->_helper->getHelper('Redirector');
		$redirector->gotoUrlAndExit($redirectUrl);
	}

	/**
	 * Wyświetlanie zmien w grafice
	 * @return void
	 */
	public function displayAction()
	{
		/* @var $viewRenderer Zend_Controller_Action_Helper_ViewRenderer */
		$viewRenderer = $this->_helper->getHelper('ViewRenderer');
		$viewRenderer->setNoRender(true);

		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();

		$image = $rq->getQuery('image');

		$model = new Tools_Model_Image($image);
		$model->setActions($rq->getParams());
		$model->display();
	}
}